package example

import (
	"time"

	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/request"
	"gitee.com/Luna-CY/Golang-Project-Template/entry/http/response"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context/contextutil"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/i18n"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"github.com/gin-gonic/gin"
)

type DetailRequest struct {
	Id string `json:"id" validate:"required" binding:"required"` // record id
}

type DetailResponse struct {
	Id         string `json:"id" validate:"required"`                 // 数据ID
	Name       string `json:"name" validate:"required"`               // 字符串字段
	Value      uint64 `json:"value" validate:"required"`              // 数值字段
	Enable     bool   `json:"enable" validate:"required"`             // 布尔字段
	Type       int    `json:"type" validate:"required" enums:"1,2,3"` // 枚举字段, enums: 1 - A, 2 - B, 3 - C
	ImageId    string `json:"image_id" validate:"required"`           // 文件字段
	ImageUrl   string `json:"image_url" validate:"required"`          // 文件字段链接
	CreateTime int64  `json:"create_time" validate:"required"`        // 创建时间
	UpdateTime int64  `json:"update_time" validate:"required"`        // 更新时间
}

// Detail Detail
// @tags category/example
// @summary detail
// @param param body DetailRequest true "request body"
// @success 200 {object} response.Response{data=nil} "请求成功. 点击查看响应结构, 展开后可切换不同Code的返回结构"
// @success 200 {object} response.Response{data=DetailResponse{}} "请求成功. 点击查看响应结构, 展开后可切换不同Code的返回结构"
// @router /example/detail [post]
func (cls *Example) Detail(c *gin.Context) (response.Code, any, errors.I18nError) {
	var body = DetailRequest{}
	if err := request.ShouldBindJSON(c, &body); nil != err {
		return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.ErrorInvalidRequest("SHGWHE_LE.E_LE.D_IL.362947", "参数错误")))
	}

	var ctx = contextutil.NewContextWithGin(c)
	data, err := cls.example.GetExampleByCondition(ctx, dao.BusinessExampleOptionWithId(body.Id))
	if nil != err {
		if err.IsType(errors.ErrorTypeRecordNotFound) {
			return response.InvalidParams, nil, errors.NewI18n(i18n.CommonIdInvalidRequest, err.Relation(errors.New(errors.ErrorTypeInvalidRequest, "SHGWHE_LE.E_LE.D_IL.432954", "example record not found: %s", body.Id)))
		}

		return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.D_IL.463005", "获取数据失败")))
	}

	var res = pointer.Default(DetailResponse{
		Id:         data.Id,
		Name:       *data.Name,
		Value:      *data.Value,
		Enable:     *data.Enable,
		Type:       int(*data.Type),
		ImageId:    *data.ImageId,
		CreateTime: *data.CreateTime,
		UpdateTime: *data.UpdateTime,
	})

	if "" != *data.ImageId {
		image, err := cls.storage.GetObjectByCondition(ctx, dao.SystemObjectOptionWithId(*data.ImageId))
		if nil != err {
			if err.IsType(errors.ErrorTypeRecordNotFound) {
				return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.D_IL.652530", "文件不存在")))
			}

			return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.D_IL.682534", "获取文件失败")))
		}

		url, err := cls.storage.GetObjectAccessedUrl(ctx, image, 10*time.Minute)
		if nil != err {
			return response.ServerInternalError, nil, errors.NewI18n(i18n.CommonIdServerInternalError, err.Relation(errors.ErrorServerInternalError("SHGWHE_LE.E_LE.D_IL.752613", "获取文件链接失败")))
		}

		res.ImageUrl = url
	}

	return response.Ok, res, nil
}
